using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WHC.Hotel.Entity;
using WHC.Hotel.IDAL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.DAL
{
	/// <summary>
	/// Room 的摘要说明。
	/// </summary>
	public class Room : BaseDAL<RoomInfo>, IRoom
	{
		#region 对象实例及构造函数

		public static Room Instance
		{
			get
			{
				return new Room();
			}
		}
		public Room() : base("KF_Room","ID")
		{
            this.IsDescending = false;
        }

		#endregion

		/// <summary>
		/// 将DataReader的属性值转化为实体类的属性值，返回实体类
		/// </summary>
		/// <param name="dr">有效的DataReader对象</param>
		/// <returns>实体类对象</returns>
		protected override RoomInfo DataReaderToEntity(IDataReader dataReader)
		{
			RoomInfo roomInfo = new RoomInfo();
			SmartDataReader reader = new SmartDataReader(dataReader);
			
			roomInfo.ID = reader.GetInt32("ID");
			roomInfo.RoomNo = reader.GetString("RoomNo");
            roomInfo.RoomType = reader.GetString("RoomType");
            roomInfo.Status = ConvertStatus(reader.GetString("Status"));
            roomInfo.BuildNum = reader.GetString("BuildNum");
            roomInfo.Floor = reader.GetString("Floor");
			roomInfo.RoomTelephone = reader.GetString("RoomTelephone");
			roomInfo.LockNumber = reader.GetString("LockNumber");
			
			return roomInfo;
		}

        private RoomStatus ConvertStatus(string status)
        {
            RoomStatus roomStatus = RoomStatus.空净房;
            try
            {
                roomStatus = (RoomStatus)Enum.Parse(typeof(RoomStatus), status);
            }
            catch
            {
            }
            return roomStatus;
        }

		/// <summary>
		/// 将实体对象的属性值转化为Hashtable对应的键值
		/// </summary>
		/// <param name="obj">有效的实体对象</param>
		/// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(RoomInfo obj)
		{
		    RoomInfo info = obj as RoomInfo;
			Hashtable hash = new Hashtable(); 
			
 			hash.Add("RoomNo", info.RoomNo);
 			hash.Add("RoomType", info.RoomType);
 			hash.Add("Status", info.Status.ToString());
            hash.Add("BuildNum", info.BuildNum);
            hash.Add("Floor", info.Floor);
 			hash.Add("RoomTelephone", info.RoomTelephone);
 			hash.Add("LockNumber", info.LockNumber);
 				
			return hash;
		}

        public List<string> GetAllRegions()
        {
            return GetFieldList("Region");
        }

        /// <summary>
        /// 获取所有房间的状态统计数据
        /// </summary>
        /// <returns></returns>
        public RoomStatusInfo GetStatus()
        {
            string sql = string.Format("select Status,count(Status) as Count from {0} group by Status", tableName);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);

            RoomStatusInfo info = new RoomStatusInfo();
            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    #region 解析各种房间状态
                    string status = dr["Status"].ToString();
                    switch (status)
                    {
                        case "空净房":
                            info.VacancyClean = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "空脏房":
                            info.VacancyDirty = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "住净房":
                            info.OccupationClean = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "住脏房":
                            info.OccupationDirty = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "维修房":
                            info.Maintain = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "锁房":
                            info.Lock = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "强制锁房":
                            info.ForceLock = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "预定":
                            info.BookIns = Convert.ToInt32(dr["Count"].ToString());
                            break;
                    } 
                    #endregion
                }
            }
            info.All = info.VacancyClean + info.VacancyDirty + info.OccupationClean + info.OccupationDirty + info.BookIns + info.Maintain+info.Lock+info.ForceLock;
            if (info.All > 0)
            {
                double rate = Convert.ToDouble(info.OccupationClean + info.OccupationClean) / info.All;
                info.InRoomRate = rate.ToString("P2");
            }
            return info;
        }

        /// <summary>
        /// 设置房间的状态
        /// </summary>
        /// <param name="roomStatus">房间状态</param>
        /// <returns></returns>
        public bool SetRoomStatus(string roomNo, string roomStatus)
        {
            string sql = string.Format("Update {0} Set Status='{1}' where RoomNo='{2}' ", 
                this.tableName, roomStatus, roomNo);

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            return db.ExecuteNonQuery(command) > 0;
        }
        
        /// <summary>
        /// 根据房间号获取对应的房间对象
        /// </summary>
        /// <param name="roomNo"></param>
        /// <returns></returns>
        public RoomInfo FindByRoomNo(string roomNo)
        {
            string condition = string.Format("RoomNo='{0}' ", roomNo);
            return this.FindSingle(condition);
        }

        /// <summary>
        /// 初始化数据库信息
        /// </summary>
        public void InitDatabase()
        {
            string storeProcedure = "USP_InitDatabase";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetStoredProcCommand(storeProcedure);
            db.ExecuteNonQuery(command);
        }

    }
}